import { LOGIN, HOME, LOGOUT, CHANGETAB } from './actionTypes'
const defaultState = {
    name: localStorage.getItem('name'),
    info: localStorage.getItem('info') && JSON.parse(localStorage.getItem('info')),
    tabState: localStorage.getItem('tabState') || 'home'
}

export default (state = defaultState, action) => {
    if (action.type === LOGIN) {
        let newState = JSON.parse(JSON.stringify(state));
        newState.name = action.name;
        localStorage.setItem('name', action.name);
        return newState;
    } else if (action.type === HOME) {
        let newState = JSON.parse(JSON.stringify(state));
        newState.info = action.data;
        localStorage.setItem('info', JSON.stringify(action.data));
        return newState;
    } else if (action.type === LOGOUT) {
        let newState = JSON.parse(JSON.stringify(state));
        newState.name = '';
        newState.info = '';
        newState.tabState = 'home'
        localStorage.removeItem('name');
        localStorage.removeItem('info');
        localStorage.removeItem('tabState')
        return newState;
    } else if (action.type === CHANGETAB) {
        let newState = JSON.parse(JSON.stringify(state));
        newState.tabState = action.tabState;
        localStorage.setItem('tabState', action.tabState)
        return newState;
    }
    return state;
}